package com.ez.supervise.action.base;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.ez.supervise.model.dto.LoginUser;
import com.ez.supervise.model.sys.Permission;
import com.ez.supervise.service.PermissionService;
import com.ez.supervise.util.Config;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author searph.wang
 * @email searph.wang@gmail.com
 * @Created at 2009-7-21
 */

public class BaseAction extends ActionSupport {

    private static final long serialVersionUID = -1861818080627613274L;
    
    private HttpServletRequest request;

    private static Map<String,Permission> menu;
    private static Map<String,Permission> subMenu;
    
    protected final Log log = LogFactory.getLog(getClass());
    protected String contextPath;
    protected LoginUser loginUser;
    protected static String baseHttp = Config.getString("httpbaseurl");
    protected static String baseHttps = Config.getString("httpsbaseurl");
    protected static String context = Config.getString("web.context");
    protected static String loginAction = Config.getString("login.action");
    protected static String webSiteLink = Config.getString("web.site.link");
    
    public static final String WEB_SITE_PATH = Config.getString("web.site.path");
    public static final String FREEMARK_TEMPLATE_PATH = Config.getString("freemark.template.path");
    
    protected PermissionService permissionService;

    
    public static final String SESSION_USER = "user";
    public static String  SESSION_SCRIPT = "script";
    public static final String UPLOAD_FILE_PATH = "upload";
    public static final String UPLOAD_FILE_NOTICE = "notice";
       
    
    public String getScript() {
       return (String) getSession().getAttribute(SESSION_SCRIPT);
    }

    public void setScript(String script) { 
	getSession().setAttribute(SESSION_SCRIPT, script);
    }

    public void setPermissionService(PermissionService permissionService) {
        this.permissionService = permissionService;
    }
    
    /**
     * 获得request
     * 
     * @return
     */
    public HttpServletRequest getRequest() {
	if(request == null) {
	    request = ServletActionContext.getRequest();
	}
	return request;
    }

    /**
     * 获得response
     * 
     * @return
     */
    public HttpServletResponse getResponse() {
	return ServletActionContext.getResponse();
    }

    /**
     * 获得session
     * 
     * @return
     */
    public HttpSession getSession() {
	return getRequest().getSession();
    }

    public String getContextPath() {
	if (contextPath == null) {
	    contextPath = getContextPath(ServletActionContext.getRequest());
	}
	return contextPath;
    }

    public static String getBaseHttp() {
	return baseHttp;
    }

    public static String getBaseHttps() {
	return baseHttps;
    }

    public static String getLoginAction() {
	return loginAction;
    }

    public void setContextPath(String base) {
	this.contextPath = base;
    }
    
    public static String getWebSiteLink() {
       return webSiteLink;
    }
    
    private String getContextPath(HttpServletRequest request) {
	return request == null ? "" : request.getContextPath();
    }

    public LoginUser getLoginUser() {
	if(loginUser == null) {
	    loginUser = (LoginUser) getSession().getAttribute(SESSION_USER);
	}
	return loginUser;
    }

    public void registerLoginUser(LoginUser loginUser) {
	this.loginUser = loginUser;
	getSession().setAttribute(SESSION_USER, loginUser);
    }

    public void clearLoginUser() {
	this.loginUser = null;
	getSession().removeAttribute(SESSION_USER);
    }
    public boolean isLogin(){
	return getLoginUser() != null;
    }

    public Map<String, Permission> getMenu() {
	if(menu == null){
	    menu =  permissionService.findAllMenu();
	}
        return menu;
    }

    
    public Map<String, Permission> getSubMenu() {
	if(subMenu == null){
	    subMenu =  permissionService.findAllSubMenu();
	}
        return subMenu;
    }
}
